//
//  ActivityDetailViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2017/12/29.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "ActivityDetailTableViewCell.h"
#import "SelectActivityNumViewController.h"
#import "ActivityInfo.h"

@interface ActivityDetailViewController ()<UIWebViewDelegate>
@property(nonatomic,weak)UIWebView *webView;
@property(nonatomic,assign)double desHeight;
@property(nonatomic,weak) UITableViewCell *detailCell;
@property (nonatomic,strong) ActivityInfo *activityInfo;
@end

@implementation ActivityDetailViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    //    [Global setStatusBarBackgroundColor:[UIColor clearColor]];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGRect rect = self.view.bounds;
    rect.size.height -= 70;
    rect.origin.y -= 20;
    self.tableView.frame = rect;
    [self creatData];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([ActivityDetailTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"ActivityDetailTableViewCell"];
}
- (void)creatData{
    YJNetWorkManger *yjNetWorkManger =  [YJNetWorkManger shareManager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setValue:[Global getUserTokenInfo] forKey:@"token"]; //用户的token
    [dic setValue:self.activityId forKey:@"fileEntryId"];
    [yjNetWorkManger requestType:POST andURL:[JSPathUtil urlAppHomeSysHomeSuggestedReading] andParmas:dic andComplition:^(id response, BOOL isuccess) {
        if (isuccess) {
            self.activityInfo = [ActivityInfo mj_objectWithKeyValues:response];
            [self creatFootView];
            [self.tableView reloadData];
        }
    }];
}
#pragma mark - creatFootView
- (void)creatFootView{
    UIView *footView = [[UIView alloc]init];
    footView.frame = CGRectMake(0, kScreenH - 80, kScreenW, 70);
    [self.view addSubview:footView];
    
    CompleteButton *completeButton = [[CompleteButton alloc]initWithFrame:CGRectMake(10, 15, kScreenW - 20, 50) bgColor:KBACKNAVIGATIONCOLOR tColor:[UIColor whiteColor] text:@"报名"];
    [completeButton addTarget:self action:@selector(signUp) forControlEvents:UIControlEventTouchUpInside];
    if ([self.activityInfo.state isEqualToString:@"0"]) {
        completeButton.backgroundColor =  [UIColor lightGrayColor];
        completeButton.userInteractionEnabled = NO;
    }
    [footView addSubview:completeButton];
}

#pragma mark - 报名
- (void)signUp{
    MyLog(@"报名");
    SelectActivityNumViewController *selctActivityVC = [[SelectActivityNumViewController alloc]init];
    selctActivityVC.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    selctActivityVC.activityInfo = self.activityInfo;
    [self presentViewController:selctActivityVC animated:YES completion:^{
        
    }];

}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section) {
        return 1;
    }
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section){
            static NSString *cellID=@"webCell";
            UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            }
            self.detailCell=cell;
            UIWebView *webview=[self.detailCell.contentView viewWithTag:1000];
            if (webview) {
                webview.frame=CGRectMake(5,30,kScreenW - 10,self.desHeight-30);
            }
            else
            {
                UIWebView *webView =[[UIWebView alloc] initWithFrame:CGRectZero];
                webView.tag=1000;
                webView.delegate = self;
                webView.userInteractionEnabled=NO;
                webView.backgroundColor=[UIColor clearColor];
                [self.webView setScalesPageToFit:YES];
                
                NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/sys/home/suggestedReading?fileEntryId=%@",URL_BASE,self.activityId]];
                NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
                [webView loadRequest:request];
                
                [cell.contentView addSubview:webView];
                self.webView = webView;
            }
            return self.detailCell;
//        }
    }else if (!indexPath.section && !indexPath.row) {
        ActivityDetailTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ActivityDetailTableViewCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.activityInfo = self.activityInfo;
        __weak typeof(self) weakSelf = self;
        cell.backActivityListBlock=^(){
            [weakSelf.navigationController popViewControllerAnimated:YES];
        };
        return cell;
    }
    
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;

        UILabel *detailLabel = [[UILabel alloc]init];
        detailLabel.frame = CGRectMake(15, 12, 200,20);
        detailLabel.textColor = KTEXTMAINCOLOR;
        detailLabel.tag = 1100;
        detailLabel.font = [UIFont systemFontOfSize:15];
        [cell.contentView addSubview:detailLabel];
    }
    UILabel *detailLabel = [cell.contentView viewWithTag:1100];
    if (indexPath.row ==1) {
        detailLabel.text = [NSString stringWithFormat:@"开始时间：%@",self.activityInfo.activeStartDate];
    }else if (indexPath.row == 2){
        detailLabel.text = [NSString stringWithFormat:@"地址：%@",self.activityInfo.address];
    }else if (indexPath.row == 3){
        detailLabel.text = [NSString stringWithFormat:@"%@人已报名",self.activityInfo.readCount];
    }else if (indexPath.row == 4){
        detailLabel.textColor = [UIColor orangeColor];
        detailLabel.text = [NSString stringWithFormat:@"价格：¥%@",self.activityInfo.fee];
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section) {
        return 30 + self.desHeight;
    }else if (!indexPath.row) {
        return 300;
    }
    return 44;
}
-  (void)webViewDidFinishLoad:(UIWebView *)webView{
    MyLog(@"--------------------网页加载结束");
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    self.desHeight = height;
    [self.tableView reloadData];
//    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
